package com.zr.mybatis.controller;

import com.zr.mybatis.entity.User;
import com.zr.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 创建用户
    @PostMapping
    public String createUser(@RequestBody User user) {
        int result = userService.addUser(user);
        return result > 0 ? "用户创建成功" : "用户创建失败";
    }

    // 根据ID删除用户
    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        int result = userService.deleteUserById(id);
        return result > 0 ? "用户删除成功" : "用户删除失败";
    }

    // 更新用户
    @PutMapping
    public String updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        return result > 0 ? "用户更新成功" : "用户更新失败";
    }

    // 根据ID查询用户
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    // 查询所有用户
    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }
}
